<?php

class m120428_155535_create_manga extends CDbMigration {

	public function safeUp() {
		$manga = array(
			'id'=>'pk',
			'title'=>'string',
			'author'=>'string',
			'artist'=>'string',
			'tags'=>'string',
			'extension'=>'varchar(4)',
			'createdAt'=>'datetime',
			'updatedAt'=>'datetime',
		);
		$this->createTable('mangas', $manga);
		$this->createIndex('index_manga_title', 'mangas', 'title');
		$this->createIndex('index_manga_author', 'mangas', 'author');
		$this->createIndex('index_manga_artist', 'mangas', 'artist');
		$this->createIndex('index_manga_updated_at', 'mangas', 'updatedAt');

		$auth = Yii::app()->authManager;

		$auth->createOperation('mangaCreate', 'Create Manga');
		$auth->createOperation('mangaUpdate', 'Update Manga');
		$auth->createOperation('mangaDelete', 'Delete Manga');
		$auth->createOperation('mangaManage', 'Manage Manga');

		$role = $auth->createRole('mangaModerator', 'Manga Moderator');
		$role->addChild('mangaCreate');
		$role->addChild('mangaUpdate');
		$role->addChild('mangaDelete');
		$role->addChild('mangaManage');

		$auth->addItemChild('admin', 'mangaModerator');
	}

	public function safeDown() {
		$this->dropTable('mangas');

		$auth = Yii::app()->authManager;

		$auth->removeItemChild('admin', 'mangaModerator');

		$auth->removeItemChild('mangaModerator', 'mangaCreate');
		$auth->removeItemChild('mangaModerator', 'mangaUpdate');
		$auth->removeItemChild('mangaModerator', 'mangaDelete');
		$auth->removeItemChild('mangaModerator', 'mangaManage');

		$auth->removeAuthItem('mangaCreate');
		$auth->removeAuthItem('mangaUpdate');
		$auth->removeAuthItem('mangaDelete');
		$auth->removeAuthItem('mangaManage');

		$auth->removeAuthItem('mangaModerator');
	}

}